﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Day
{
    /// <summary>
    /// 项目分时数据
    /// </summary>
    public class DayTradeInfo
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }

        public int State { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }



        ///// <summary>
        ///// 删除当日分时记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = from t in db.DayTradeInfo where t.TradeHistoryId == tradeHistoryId select t;
        //    foreach (Data.Day.DayTradeInfo c in query)
        //    {
        //        db.DayTradeInfo.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 保存账号分时数据OK
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitDayStateChange(int tradeHistoryId, string pdate, DB.DBManager db)
        //{
        //    List<Data.ServerData.T_Account> serverAccounts = new Data.ServerData.T_Account().GetList();
        //    Sys.Account _account = new Sys.Account();
        //    Data.ServerData.T_TradeDetail_History h = new ServerData.T_TradeDetail_History();
        //    foreach (Data.ServerData.T_Account account in serverAccounts)
        //    {
        //        for (int i = 0; i < 8; i++)//这个i就是阶段的代号
        //        {
        //            DayTradeInfo info = new DayTradeInfo();
        //            info.AccountId = _account.GetAccountId(account.LoginAccount);
        //            info.State = i;
        //            info.BillCount = h.GetStateDayBillCount(pdate, account.Id, i, db);
        //            info.DayCount = h.GetDayStateCloseProfit(pdate, account.Id, i, db) - h.GetDayStateCommission(pdate, account.Id, i, db);
        //            info.Free = h.GetDayStateCommission(pdate, account.Id, i, db);
        //            info.LoseCount = h.GetDayStateLoseCount(pdate, account.Id, i, db);
        //            info.Profit = h.GetDayStateCloseProfit(pdate, account.Id, i, db);
        //            info.SumLost = h.GetStateDayAllLose(pdate, account.Id, i, db);
        //            info.SumWin = h.GetStateDayAllWin(pdate, account.Id, i, db);
        //            info.TradeHistoryId = tradeHistoryId;
        //            info.UsedMargin = h.GetDayStateMargin(pdate, account.Id, i, db);
        //            info.WinCount = h.GetStateDayWinCount(pdate, account.Id, i, db);
        //            db.DayTradeInfo.Add(info);
        //        }
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 查询账户某天的分时交易信息
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="accountid"></param>
        ///// <returns></returns>
        //public List<DayTradeInfo> GetAccountDayTradeInfos(int tradeHistoryId, int accountid, DB.DBManager db)
        //{
        //    List<DayTradeInfo> infos = new List<DayTradeInfo>();
        //    var query = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
        //    if (query.Count() > 0)
        //    {
        //        infos = query.ToList();
        //    }
        //    return infos;
        //}
        ///// <summary>
        ///// 查询账户某天的分时交易信息
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="accountid"></param>
        ///// <returns></returns>
        //public List<DayTradeInfo> GetAccountDayTradeInfos(int tradeHistoryId, int accountid, DB.DBManager db, bool win)
        //{
        //    List<DayTradeInfo> infos = new List<DayTradeInfo>();
        //    if (win)
        //    {
        //        infos = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.Profit > 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
        //    }
        //    else
        //    {
        //        infos = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.Profit <= 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
        //    }


        //    return infos;
        //}
    }
}